package com.neuedu.controller.backend;

import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "/manager/user",description = "后台管理员相关接口")
@Controller
@RequestMapping("/manager/user")
public class ManagerUserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录",notes = "登录信息描述",httpMethod = "POST")
    @RequestMapping("login")
    @ResponseBody
    public JSONData<UserInfo> login(HttpSession session,String username,String password){
        JSONData<UserInfo> data = userService.login(username,password);
        //1、判断用户是否登录成功
        if(data.getCode()==0){
            //2、判断其是管理员：0还是普通用户：1
            UserInfo userInfo = data.getData();
            if(userInfo.getRole()== Const.ADMINROLE){
                session.setAttribute(Const.CURRENT_USERINFO,userInfo);
            }else{
                return JSONData.buildError("不是管理员，无法登录！");
            }
        }
        return data;
    }
    @ApiOperation(value = "查询所有用户信息",notes = "查询所有用户信息",httpMethod = "POST")
    @RequestMapping("getAllUserInfo")
    @ResponseBody
    public JSONData<List<UserInfo>> getAllUserInfo(){
        JSONData<List<UserInfo>> data = userService.getAllUserInfo();
        return data;
    }
    @ApiOperation(value = "通过用户ID删除指定用户信息",notes = "通过用户ID删除指定用户信息",httpMethod = "POST")
    @RequestMapping("delUserInfoById")
    @ResponseBody
    public JSONData<String> delUserInfoById(Integer id){
        JSONData<String> data = userService.delUserInfoById(id);
        return data;
    }

}
